package com.data;

import com.data.Course;
import com.data.Login;
import com.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseDAO {
    public List<Course> display(){
        List<Course> courses=new ArrayList<>();
        Map<String, Object> map = null;
        try {
            List<Map<String, Object>> list = DBUtil.query("course",null);
            int size = list.size();
            for(int i=0;i<size;i++){
                map = list.get(i);
                Course course  = new Course(map.get("cId").toString(),
                        map.get("cName").toString(),
                        map.get("cNum").toString(),
                        map.get("cType").toString()
                        );
                courses.add(course);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        return courses;
    }
    public void add(String cName,int cNum,String cType){
            int cid = 1;
            String s = "select max(cid) from course";
            List<Map<String, Object>> list=null;
            try {
                 list = DBUtil.query(s);
            }
            catch (Exception e){
                e.printStackTrace();
            }
            if (list.size() > 0) {
                Map<String, Object> map = list.get(0);
                cid = ((Integer)map.get("max(cid)")).intValue()+1;
            }
            if (cType.equals("1")) {
                cType = "必修";
            } else {
                cType = "选修";
            }
            Map<String, Object> valueMap = new HashMap<>();
            valueMap.put("cId", cid);
            valueMap.put("cName", cName);
            valueMap.put("cNum", cNum);
            valueMap.put("cType", cType);
            try {
                int count = DBUtil.insert("course", valueMap);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    public void delete(int cid){
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("cId",cid);
        try {
            DBUtil.delete("course", whereMap);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public int totalSize(){
        String sql ="select count(*) from course";
        List<Map<String, Object>> list=new ArrayList<>();
        try {
            list=DBUtil.query(sql);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return Integer.parseInt(list.get(0).get("count(*)").toString());
    }
    public List<Course> getSomeCourse(int pageNo,int pageSize){
        int start=(pageNo-1)*pageSize;

        String sql="select * from course limit ?,?";
        Object[] o=new Object[2];
        o[0]=start;
        o[1]=pageSize;

        Map<String, Object> map = null;
        List<Course> courses=new ArrayList<>();
        try {
            List<Map<String, Object>> list = DBUtil.executeQuery(sql,o);
            int size = list.size();
            for(int i=0;i<size;i++){
                map = list.get(i);
                Course course  = new Course(map.get("cId").toString(),
                        map.get("cName").toString(),
                        map.get("cNum").toString(),
                        map.get("cType").toString()
                );
                courses.add(course);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        return courses;
    }
    public List<Course> getSomeCourse(int pageNo,int pageSize,String s){
        int start=(pageNo-1)*pageSize;

        String sql="select * from course "+s+" limit ?,?";
        Object[] o=new Object[2];
        o[0]=start;
        o[1]=pageSize;

        Map<String, Object> map = null;
        List<Course> courses=new ArrayList<>();
        try {
            List<Map<String, Object>> list = DBUtil.executeQuery(sql,o);
            int size = list.size();
            for(int i=0;i<size;i++){
                map = list.get(i);
                Course course  = new Course(map.get("cId").toString(),
                        map.get("cName").toString(),
                        map.get("cNum").toString(),
                        map.get("cType").toString()
                );
                courses.add(course);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        return courses;
    }
}
